app.controller(ꞌBoardControllerꞌ)   B
last analyzed

Complexity

Conditions 1
Paths 4

Size

Total Lines 228

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 228
rs 8.2857
c 0
b 0
f 0
cc 1
nc 4
nop 11

27 Functions

Rating   Name   Duplication   Size   Complexity  
A ��) 0 3 1
A ��) 0 4 1
A ��) 0 4 1
A ��) 0 5 1
A ��) 0 17 1
A ��) 0 3 1
A ��) 0 6 2
A ��) 0 11 1
A ��) 0 4 1
A ��) 0 4 1
A ��) 0 3 1
A ��) 0 3 1
A ��) 0 7 1
B ��) 0 18 6
A ��) 0 7 2
A ��) 0 4 1
A ��) 0 10 2
A ��) 0 9 1
A ��) 0 4 1
A ��) 0 7 2
A ��) 0 3 1
A ��) 0 7 1
A ��) 0 4 1
A ��) 0 5 1
A ��) 0 3 1
A ��) 0 7 1
A ��) 0 7 1

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
/*
2
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
3
 *
4
 * @author Julius Härtl <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *  
8
 *  This program is free software: you can redistribute it and/or modify
9
 *  it under the terms of the GNU Affero General Public License as
10
 *  published by the Free Software Foundation, either version 3 of the
11
 *  License, or (at your option) any later version.
12
 *  
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU Affero General Public License for more details.
17
 *  
18
 *  You should have received a copy of the GNU Affero General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *  
21
 */
22
23
app.controller('BoardController', function ($rootScope, $scope, $stateParams, StatusService, BoardService, StackService, CardService, LabelService, $state, $transitions, $filter) {
24
25
	$scope.sidebar = $rootScope.sidebar;
26
27
	$scope.id = $stateParams.boardId;
28
	$scope.status = {
29
		addCard: [],
30
	};
31
	$scope.newLabel = {};
32
	$scope.status.boardtab = $stateParams.detailTab;
33
34
	$scope.stackservice = StackService;
35
	$scope.boardservice = BoardService;
36
	$scope.cardservice = CardService;
37
	$scope.statusservice = StatusService.getInstance();
38
	$scope.labelservice = LabelService;
39
	$scope.defaultColors = ['31CC7C', '317CCC', 'FF7A66', 'F1DB50', '7C31CC', 'CC317C', '3A3B3D', 'CACBCD'];
40
41
	$scope.search = function (searchText) {
42
		$scope.searchText = searchText;
43
		$scope.refreshData();
44
	};
45
46
	$scope.board = BoardService.getCurrent();
47
	StackService.clear(); //FIXME: Is this still needed?
48
	$scope.statusservice.retainWaiting();
49
	$scope.statusservice.retainWaiting();
50
51
	// FIXME: ugly solution for archive
52
	$scope.$state = $stateParams;
53
	$scope.filter = $stateParams.filter;
54
	$scope.$watch('$state.filter', function (name) {
55
		$scope.filter = name;
56
	});
57
	$scope.switchFilter = function (filter) {
58
		$state.go('.', {filter: filter}, {notify: false});
59
		$scope.filter = filter;
60
	};
61
	$scope.$watch('filter', function (name) {
62
		if (name === "archive") {
63
			$scope.loadArchived();
64
		} else {
65
			$scope.loadDefault();
66
		}
67
	});
68
69
70
	$scope.stacksData = StackService;
71
	$scope.stacks = {};
72
	$scope.$watch('stacksData', function (value) {
0 ignored issues
show
Unused Code introduced by
The parameter value is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
73
		$scope.refreshData();
74
	}, true);
75
	$scope.refreshData = function () {
76
		if ($scope.filter === "archive") {
77
			$scope.filterData('-lastModified', $scope.searchText);
78
		} else {
79
			$scope.filterData('order', $scope.searchText);
80
		}
81
	};
82
	$scope.checkCanEdit = function () {
83
		return !$scope.archived;
84
	};
85
86
	// filter cards here, as ng-sortable will not work nicely with html-inline filters
87
	$scope.filterData = function (order, text) {
88
		if ($scope.stacks === undefined)
89
			return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
90
		angular.copy(StackService.getAll(), $scope.stacks);
91
		angular.forEach($scope.stacks, function (value, key) {
92
			var cards = $filter('cardSearchFilter')(value.cards, text);
93
			cards = $filter('orderBy')(cards, order);
94
			$scope.stacks[key].cards = cards;
95
		});
96
	};
97
98
	$scope.loadDefault = function () {
99
		StackService.fetchAll($scope.id).then(function (data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
100
			$scope.statusservice.releaseWaiting();
101
		}, function (error) {
102
			$scope.statusservice.setError('Error occured', error);
103
		});
104
	};
105
106
	$scope.loadArchived = function () {
107
		StackService.fetchArchived($scope.id).then(function (data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
108
			$scope.statusservice.releaseWaiting();
109
		}, function (error) {
110
			$scope.statusservice.setError('Error occured', error);
111
		});
112
	};
113
114
	// Handle initial Loading
115
	BoardService.fetchOne($scope.id).then(function (data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
116
		BoardService.getPermissions();
117
		$scope.statusservice.releaseWaiting();
118
	}, function (error) {
119
		$scope.statusservice.setError('Error occured', error);
120
	});
121
122
	BoardService.searchUsers('%25');
123
124
	$scope.searchForUser = function (search) {
125
		if (search == "") {
126
			search = "%25";
127
		}
128
		BoardService.searchUsers(search);
129
	};
130
131
	$scope.newStack = {'boardId': $scope.id};
132
	$scope.newCard = {};
133
134
	// Create a new Stack
135
	$scope.createStack = function () {
136
		StackService.create($scope.newStack).then(function (data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
137
			$scope.newStack.title = "";
138
		});
139
	};
140
141
	$scope.createCard = function (stack, title) {
142
		var newCard = {
143
			'title': title,
144
			'stackId': stack,
145
			'type': 'plain'
146
		};
147
		CardService.create(newCard).then(function (data) {
148
			$scope.stackservice.addCard(data);
149
			$scope.newCard.title = "";
150
		});
151
	};
152
153
	$scope.cardDelete = function (card) {
154
		CardService.delete(card.id);
155
		StackService.removeCard(card);
156
	};
157
	$scope.cardArchive = function (card) {
158
		CardService.archive(card);
159
		StackService.removeCard(card);
160
	};
161
	$scope.cardUnarchive = function (card) {
162
		CardService.unarchive(card);
163
		StackService.removeCard(card);
164
	};
165
166
	$scope.labelDelete = function (label) {
167
		LabelService.delete(label.id);
168
		// remove from board data
169
		var i = BoardService.getCurrent().labels.indexOf(label);
170
		BoardService.getCurrent().labels.splice(i, 1);
171
		// TODO: remove from cards
172
	};
173
	$scope.labelCreate = function (label) {
174
		label.boardId = $scope.id;
175
		LabelService.create(label);
176
		BoardService.getCurrent().labels.push(label);
177
		$scope.status.createLabel = false;
178
		$scope.newLabel = {};
179
	};
180
	$scope.labelUpdate = function (label) {
181
		label.edit = false;
182
		LabelService.update(label);
183
	};
184
185
	$scope.aclAdd = function (sharee) {
186
		sharee.boardId = $scope.id;
187
		BoardService.addAcl(sharee);
188
		$scope.status.addSharee = null;
189
	};
190
	$scope.aclDelete = function (acl) {
191
		BoardService.deleteAcl(acl);
192
	};
193
	$scope.aclUpdate = function (acl) {
194
		BoardService.updateAcl(acl);
195
	};
196
197
198
	// settings for card sorting
199
	$scope.sortOptions = {
200
		itemMoved: function (event) {
201
			event.source.itemScope.modelValue.status = event.dest.sortableScope.$parent.column;
202
			var order = event.dest.index;
203
			var card = event.source.itemScope.c;
204
			var newStack = event.dest.sortableScope.$parent.s.id;
205
			var oldStack = card.stackId;
206
			card.stackId = newStack;
207
			CardService.update(card);
208
			CardService.reorder(card, order).then(function (data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
209
				StackService.addCard(card);
210
				StackService.reorder(card, order);
211
				StackService.removeCard({
212
					id: card.id,
213
					stackId: oldStack
214
				});
215
			});
216
		},
217
		orderChanged: function (event) {
218
			var order = event.dest.index;
219
			var card = event.source.itemScope.c;
220
			var stack = event.dest.sortableScope.$parent.s.id;
0 ignored issues
show
Unused Code introduced by
The variable stack seems to be never used. Consider removing it.
Loading history...
221
			CardService.reorder(card, order).then(function (data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
222
				StackService.reorder(card, order);
223
				$scope.refreshData();
224
			});
225
		},
226
		scrollableContainer: '#board',
227
		containerPositioning: 'relative',
228
		containment: '#board',
229
		// auto scroll on drag
230
		dragMove: function (itemPosition, containment, eventObj) {
231
			if (eventObj) {
232
				var container = $("#board");
233
				var offset = container.offset();
234
				var targetX = eventObj.pageX - (offset.left || container.scrollLeft());
235
				var targetY = eventObj.pageY - (offset.top || container.scrollTop());
236
				if (targetX < offset.left) {
237
					container.scrollLeft(container.scrollLeft() - 50);
238
				} else if (targetX > container.width()) {
239
					container.scrollLeft(container.scrollLeft() + 50);
240
				}
241
				if (targetY < offset.top) {
242
					container.scrollTop(container.scrollTop() - 50);
243
				} else if (targetY > container.height()) {
244
					container.scrollTop(container.scrollTop() + 50);
245
				}
246
			}
247
		}
248
	};
249
250
});
251